﻿/*
 * Authors:    Kęstutis Džiautas
 *             Edmundas Matusevičius
 * University: VU, MIF, SE, 4 course
 * Date:       2010-10-23
 * Version:    1.0
 */

using System;
using System.Collections.Generic;

namespace LexAnalizer {
    public class TreeNode {
        public TreeNode() {
            data = null;
            parent = null;
            childs = new List<TreeNode>();
        }

        /// <summary>
        /// Checkes if node has parent.
        /// </summary>
        /// <returns>true if has, false otherwise.</returns>
        public bool hasParent() {
            return parent != null ? true : false;
        }

        /// <summary>
        /// Checks if node has childs.
        /// </summary>
        /// <returns>true if has, false otherwise.</returns>
        public bool hasChilds() {
            return childs.Count == 0 ? true : false;
        }

        /// <summary>
        /// Sets node's data.
        /// </summary>
        /// <param name="data">Data object to be set.</param>
        public void setData(TreeNodeData data) {
            this.data = data;
        }

        /// <summary>
        /// Returns node's data.
        /// </summary>
        /// <returns>Data object of this node.</returns>
        public TreeNodeData getData() {
            return data;
        }

        /// <summary>
        /// Sets node's parent.
        /// </summary>
        /// <param name="parent">Node object of the node's parent.</param>
        public void setParent(TreeNode parent) {
            this.parent = parent;
            // if parent exists than this node becomes his child
            if (parent != null) {
                parent.addChild(this);
            }
        }

        /// <summary>
        /// Returns node's parent.
        /// </summary>
        /// <returns>Parent object of this node.</returns>
        public TreeNode getParent() {
            return parent;
        }

        /// <summary>
        /// Returns the count of childs.
        /// </summary>
        /// <returns>int number of the childs.</returns>
        public int getChildsCount() {
            return childs.Count;
        }

        /// <summary>
        /// Adds child.
        /// </summary>
        /// <param name="child">Child object to be added.</param>
        public void addChild(TreeNode child) {
            childs.Add(child);
        }

        /// <summary>
        /// Removes child.
        /// </summary>
        /// <param name="child">Child object to be removed.</param>
        public void removeChild(TreeNode child) {
            childs.Remove(child);
            // after remove, child does not have parent anymore
            child.setParent(null);
        }

        /// <summary>
        /// Node's data object.
        /// </summary>
        private TreeNodeData data;
        /// <summary>
        /// Node's parent's object.
        /// </summary>
        private TreeNode parent;
        /// <summary>
        /// Childs consist of the list of other nodes.
        /// </summary>
        private List<TreeNode> childs;

        public List<TreeNode> Childs
        {
            get { return childs; }
        }
    }
}
